<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var generateParenthesis = function (n) {
        let res = []
        function backTracking(str, leftCount, rightCount) {
          //终止条件
          if (leftCount > n) return
          if (leftCount < rightCount) return
          if (leftCount + rightCount == 2 * n) {
            return res.push(str)
          }
          backTracking(str + '(', leftCount + 1, rightCount)
          backTracking(str + ')', leftCount, rightCount + 1)
        }
        backTracking('', 0, 0)
        return res
      }
    </script>
  </body>
</html>
